import 'package:flutter/material.dart';
import 'package:expandable/expandable.dart';
import 'package:x3_base_package/x3_base_package.dart';

class BusinessCustomerDetailsCouponsItem extends StatefulWidget {

  BusinessCustomerDetailsCouponsItem({Key key, @required this.item, @required this.couponState}) : super(key: key);

  final Map<String, dynamic> item;

  final String couponState;

  @override
  _BusinessCustomerDetailsCouponsItemState createState() => _BusinessCustomerDetailsCouponsItemState();

}

class _BusinessCustomerDetailsCouponsItemState extends State<BusinessCustomerDetailsCouponsItem> {

  final ExpandableController _controller = new ExpandableController();

  @override
  void initState() {
    super.initState();
    this._controller.addListener(() {
      setState(() {});
    });
  }

  @override
  Widget build(BuildContext context) {
    return ExpandablePanel(
      header: _buildExpansionHeader(widget.item),
      expanded: _buildExpansionContent(widget.item),
      theme: ExpandableThemeData(hasIcon: false),
      builder: (_, collapsed, expanded) {
        return Expandable(
          expanded: expanded,
          theme: const ExpandableThemeData(crossFadePoint: 0),
        );
      },
      controller: _controller,
    );
  }

  Widget _buildCouponValueWidget(dynamic item) {
    TextStyle textStyle = widget.couponState == '0' ? Theme.of(context).textTheme.bodyText1 : Theme.of(context).textTheme.subtitle2;
    String couponValue = BaseUtils.getKeyData(item, 'couponValue').toString();
    return Container(
      width: 68,
      alignment: Alignment.center,
      child: Row(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.end,
        children: [
          Padding(
            padding:  EdgeInsets.only(bottom: 2),
            child: Text('¥', style: textStyle.copyWith(fontSize: 16)),
          ),
          Text(couponValue, style: textStyle.copyWith(fontSize: 24.0)),
        ],
      ),
    );
  }

  Widget _buildCouponContentWidget(dynamic item) {
    String couponTypeName = BaseUtils.getKeyData(item, 'coupontypeName').toString();
    TextStyle textStyle = Theme.of(context).textTheme.subtitle2;
    TextStyle blackTextStyle = widget.couponState == '0' ? Theme.of(context).textTheme.bodyText2 : textStyle;
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(couponTypeName, style: blackTextStyle),
            SizedBox(height: 4.0),
            _buildCouponContentCodeWidget(item, blackTextStyle),
          ],
        ),
        _buildCouponContentDateWidget(item, textStyle),
      ],
    );
  }

  Widget _buildExpansionHeader(dynamic item) {
    return Container(
      height: 73.0,
      child: Row(
        children: [
          _buildCouponValueWidget(item),
          Container(margin: const EdgeInsets.symmetric(horizontal: 10.0), width: 1, color: Theme.of(context).scaffoldBackgroundColor),
          SizedBox(width: 2),
          Expanded(child: _buildCouponContentWidget(item)),
          _buildListItemIconWidget(),
        ],
      ),
    );
  }

  Widget _buildCouponContentCodeWidget(dynamic item, TextStyle blackTextStyle) {
    String couponCode = BaseUtils.getKeyData(item, 'couponCode').toString();
    return Row(
      children: [
        Expanded(child: BaseComponentUtils.buildTextSpanWidget(['券号:', couponCode], styles: {0: blackTextStyle, 1: blackTextStyle}, maxLines: 1)),
        SizedBox()
      ],
    );
  }

  Widget _buildCouponContentDateWidget(dynamic item, TextStyle textStyle) {
    IconData iconData = this._controller.expanded ? BaseIconData.x3TopCircleArrow : BaseIconData.x3BottomCircleArrow;
    String couponValidBegdate = BaseUtils.getKeyData(item, 'couponValidBegdate').toString();
    String couponValidEnddate = BaseUtils.getKeyData(item, 'couponValidEnddate').toString();
    String initDate = BaseTransformUtil.stringToDateTimeStamp(couponValidBegdate) + ' 至 ' + BaseTransformUtil.stringToDateTimeStamp(couponValidEnddate);
    return Row(
      children: [
        Expanded(child: BaseComponentUtils.buildTextSpanWidget(['期限:', initDate], styles: {0: textStyle, 1: textStyle}, maxLines: 2)),
        widget.couponState == '0' ? Icon(iconData, size: 15.0, color: Color(0xFFCCCCCC)) : SizedBox(),
      ],
    );
  }

  Widget _buildExpansionContent(dynamic item) {
    String couponRemark = BaseUtils.getKeyData(item, 'couponRemark').toString();
    return Container(
      width: double.infinity,
      margin: const EdgeInsets.only(top: 10.0),
      padding: const EdgeInsets.only(top: 10.0),
      decoration: new BoxDecoration(border: Border(top: BorderSide(color: Theme.of(context).scaffoldBackgroundColor))),
      child: Text(couponRemark, style: Theme.of(context).textTheme.subtitle2),
    );
  }

  Widget _buildListItemIconWidget() {
    return widget.couponState == '0'
        ? SizedBox(width: 0)
        : widget.couponState == '1'
            ? Image.asset(BaseAssets.couponUsed)
            : Image.asset(BaseAssets.couponExpired);
  }
}
